<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use app\common\model\hfztcy\LibVideoAuth;
use common\Common;


/**
 * 视频认证套餐
 * @name VideoAuthPackage.php
 * @package hfztcy
 * @videoAuthPackage controller
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-6-5
 */
class VideoAuthPackage extends AdminController
{
    protected $model;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new LibVideoAuth();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 列表页
     */
    function videoAuthPackageList()
    {
        $this->getSetMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('VideoAuthPackage/videoAuthPackageList/videoAuthPackageList');
    }

    /**
     * 添加页面
     */
    function addVideoAuthPackage()
    {
        $this->getMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "添加页面", 1, 'view');
        return $this->fetch('VideoAuthPackage/addVideoAuthPackage/addVideoAuthPackage');
    }

    /**
     * 添加
     */
    function insertVideoAuthPackage()
    {
        $videoAuthPackageInfo = $this->getArgsList($this, array('title', 'money', 'content'), false);
//        $res = $this->model->findVideoAuth(['title' => $videoAuthPackageInfo['title']]);
//        if ($res['errorCode'] == 0) {
//            echo json_encode(Common::errorArray(1, "套餐已存在", false));
//            die;
//        }
        $verifyRes = $this->model->goCheck($videoAuthPackageInfo);
        if ($verifyRes['errorCode'] == 0) {
            $result = $this->model->addVideoAuth($videoAuthPackageInfo);
            if ($result['errorCode'] == 1) {
                echo json_encode(Common::errorArray(1, $result['errorInfo'], false));
                die;
            }
        } else {
            echo json_encode(Common::errorArray(1, $verifyRes['errorInfo'], false));
            die;
        }

        $this->log(__CLASS__, __FUNCTION__, "添加", 0, 'add');
        echo json_encode($result);
    }

    /**
     * 编辑页面
     */
    function editVideoAuthPackage()
    {
        $this->getMenu($this);
        $videoAuthPackageInfo = $this->model->findVideoAuth(['id' => input("id")]);
        $this->assign('videoAuthPackageInfo', $videoAuthPackageInfo['data']);
        $this->log(__CLASS__, __FUNCTION__, "编辑页面", 1, 'view');
        return $this->fetch('VideoAuthPackage/editVideoAuthPackage/editVideoAuthPackage');
    }

    /**
     * 更新
     */
    function updateVideoAuthPackage()
    {
        $videoAuthPackageInfo = $this->getArgsList($this, array('id', 'title', 'money', 'content'), false);
        $id = $videoAuthPackageInfo['id'];
//        $res = $this->model->findVideoAuth(['title' => $videoAuthPackageInfo['title']]);
//        if ($res['errorCode'] == 0 && $res['data']['id'] != $id) {
//            echo json_encode(Common::errorArray(1, "套餐已存在", false));
//            die;
//        }
        $verifyRes = $this->model->goCheck($videoAuthPackageInfo, 'edit');
        if ($verifyRes['errorCode'] == 0) {
            $result = $this->model->updateVideoAuth(array("id" => $id), $videoAuthPackageInfo);
            if ($result['errorCode'] == 1) {
                echo json_encode(Common::errorArray(1, $result['errorInfo'], false));
                die;
            }
        } else {
            echo json_encode(Common::errorArray(1, $verifyRes['errorInfo'], false));
            die;
        }

        $this->log(__CLASS__, __FUNCTION__, "更新", 0, 'update');
        echo json_encode($result);
    }

    /**
     * 删除
     */
    function deleteVideoAuthPackage()
    {
        $id = input("id");
        $videoAuthPackageRes = $this->model->findVideoAuth(['id' => $id]);
        if ($videoAuthPackageRes['errorCode'] == 0) {
            $result = $this->model->deleteVideoAuth($id);
            echo json_encode($result);
            die();
        }
        echo json_encode(Common::errorArray(1, '不存在', false));
        die();
    }

    /**
     * 分页查询
     */
    function pagingVideoAuthPackage()
    {
        $args = $this->getArgsList($this, ['title']);
        $page = $this->getPageInfo();
        $where = [];
        if (!empty($args['title'])) $where[] = "`title` like '%" . $args['title'] . "%'";
        $result = $this->model->pagingVideoAuth($page, implode(' AND ', $where));
        echo json_encode($result);
    }
}

?>